package com.example.testdemo.ripple;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by 颜 on 04.26.026.
 * 水波纹效果TextView
 */

public class RippleTextView extends AppCompatTextView {
    private Paint mPaint;
    private int spacing;//两个波纹的间距
    private int progress;//记录波纹半径
    private boolean isDown;//记录手指是否在按钮上
    private int count;//记录波纹数量
    private boolean isGone;//记录波纹是否全部消失

    public RippleTextView(Context context) {
        super(context);
    }

    public RippleTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(2);
        mPaint.setStyle(Paint.Style.STROKE);
        //设置间距为15dp
        spacing = dp2px(context, 15);
        count = 1;
    }

    public RippleTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isDown) {
            for (int i = 0; i < count; i++) {
                canvas.drawCircle(getWidth() / 2, getHeight() / 2, progress + spacing * i, mPaint);
            }
            change();
        }else {
            for (int i = 0; i < count; i++) {
                canvas.drawCircle(getWidth() / 2, getHeight() / 2, progress + spacing * i, mPaint);
            }
            if (isGone) {
                changeAdd();
            }
        }
    }

    /**
     * 手指离开界面后，一直绘制圆圈直到圆都消失
     */
    private void changeAdd() {
        progress += 4;
        if (progress > getWidth() / 2){
            count = 1;
            progress = 0;
            isGone = false;
        }
        postInvalidate();
    }

    /**
     * 改变参数
     */
    private void change() {
        if (progress >= spacing) {
            progress = 0;
            if (count >= 7) {
                count = 7;
            } else {
                count += 1;
            }
        } else {
            progress += 4;
        }

        isGone = true;
        postInvalidate();
    }


    /**
     * 监听手势
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isDown = true;
                postInvalidate();
                break;
            case MotionEvent.ACTION_UP:
                isDown = false;
                break;
        }
        return true;
    }

    /**
     * convert px to its equivalent dp
     * <p>
     * 将px转换为与之相等的dp
     */
    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }


    /**
     * convert dp to its equivalent px
     * <p>
     * 将dp转换为与之相等的px
     */
    public static int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

}
